//
//  CreditCardViewController.m
//  联付商圈
//
//  Created by apple on 17/7/22.
//  Copyright © 2017年 lianfu. All rights reserved.
//
#define WIDTH (SCREENW - 20) / 2
#import "CreditCardViewController.h"
#import "BankCollectionReusableView.h"
#import "CreditCardCollectionViewCell.h"

@interface CreditCardViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UILabel *hintLabel;//提示
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) NSMutableArray *bankArr;
@property (nonatomic, strong) NSMutableArray *activityArr;
@property (nonatomic, strong) NSMutableArray *imgArr;
@end

@implementation CreditCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self changeNavigationController];
    [self buildMainInterface];
//    [self addLineView];
}

- (void)changeNavigationController
{
    self.navigationController.navigationBar.hidden = NO;

    [self.navigationItem setTitle:@"信用卡申请"];
}

- (void)loadData
{
    self.bankArr = [NSMutableArray arrayWithObjects:@"招商银行", @"民生银行", @"平安银行", @"广发银行", @"光大银行", @"中信银行", @"浦发银行", @"交通银行", nil];
    self.activityArr = [NSMutableArray arrayWithObjects:@"刷卡送1000积分", @"首年免年费", @"0.5%旅游消费回馈", @"自选商户3倍积分", @"10元抢电影票", @"IMAX五折观影", @"指定五星酒店'住3付2'", @"成功核卡送50元刷卡金",nil];
}

- (void)buildMainInterface
{
    self.view.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.00];
    
    _hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 64, SCREENW, 30)];
    _hintLabel.text = @"信用卡申请不需要任何费用,谨防电话诈骗!";
    _hintLabel.textAlignment = 1;
    _hintLabel.backgroundColor = [UIColor colorWithRed:1.00 green:0.89 blue:0.56 alpha:1.00];
    _hintLabel.font = [UIFont systemFontOfSize:15 * FONTSIZE];
    [self.view addSubview:_hintLabel];
    
    _backView = [[UIView alloc] init];
    _backView.backgroundColor = [UIColor whiteColor];
    _backView.layer.cornerRadius = 5;
    [self.view addSubview:_backView];
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsMake(104, 10, 10, 10));
    }];
    
    UILabel *recommendLabel = [[UILabel alloc] init];
    recommendLabel.font = [UIFont systemFontOfSize:20 * FONTSIZE];
    recommendLabel.text = @"   推荐银行";
    recommendLabel.layer.cornerRadius = 5;
    [_backView addSubview:recommendLabel];
    [recommendLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backView.mas_left).offset(0);
        make.right.equalTo(_backView.mas_right).offset(0);
        make.top.equalTo(_backView.mas_top).offset(0);
        make.height.mas_offset(50 * FONTSIZE);
    }];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.itemSize = CGSizeMake(WIDTH, 90 * FONTSIZE);
    
    UICollectionView *bankCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50 * FONTSIZE, SCREENW - 20, SCREENH - 114 - 50 * FONTSIZE) collectionViewLayout:flowLayout];
    bankCollectionView.delegate = self;
    bankCollectionView.dataSource = self;
    
    //设置两个圆角
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bankCollectionView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5,5)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = bankCollectionView.bounds;
    maskLayer.path = maskPath.CGPath;
    bankCollectionView.layer.mask = maskLayer;
    bankCollectionView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.00];
    
    [_backView addSubview:bankCollectionView];
    
    [bankCollectionView registerClass:[CreditCardCollectionViewCell class] forCellWithReuseIdentifier:@"recommendCell"];
    [bankCollectionView registerClass:[BankCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.bankArr.count - 2;
    } else {
        return self.bankArr.count - 6;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CreditCardCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"recommendCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    if (indexPath.section == 0) {
        cell.bankName.text = [self.bankArr objectAtIndex:indexPath.row];
        cell.activityTxt.text = [self.activityArr objectAtIndex:indexPath.row];
    } else {
        cell.bankName.text = [self.bankArr objectAtIndex:indexPath.row + 6];
        cell.activityTxt.text = [self.activityArr objectAtIndex:indexPath.row + 6];
    }
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        BankCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.00];
        return reusableView;
    }
    return nil;
}

-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(WIDTH, 10 * FONTSIZE);
}

- (void)addLineView
{
    UIView *lineView1 = [[UIView alloc] init];
    lineView1.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.00];
    [_backView addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_backView.mas_top).offset(50 * FONTSIZE);
        make.left.equalTo(_backView.mas_left).offset(0);
        make.right.equalTo(_backView.mas_right).offset(0);
        make.height.mas_offset(1.5 * FONTSIZE);
    }];
    
    UIView *lineView2 = [[UIView alloc] init];
    lineView2.backgroundColor = lineView1.backgroundColor;
    [_backView addSubview:lineView2];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView1.mas_bottom).offset(90 * FONTSIZE);
        make.left.equalTo(_backView.mas_left).offset(0);
        make.right.equalTo(_backView.mas_right).offset(0);
        make.height.mas_offset(1.5 * FONTSIZE);
    }];
    
    UIView *lineView3 = [[UIView alloc] init];
    lineView3.backgroundColor = lineView2.backgroundColor;
    [_backView addSubview:lineView3];
    [lineView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView2.mas_bottom).offset(90 * FONTSIZE);
        make.left.equalTo(_backView.mas_left).offset(0);
        make.right.equalTo(_backView.mas_right).offset(0);
        make.height.mas_offset(1.5 * FONTSIZE);
    }];
    
    UIView *verticalLine1 = [[UIView alloc] init];
    verticalLine1.backgroundColor = lineView3.backgroundColor;
    [_backView addSubview:verticalLine1];
    [verticalLine1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView1.mas_bottom).offset(0);
        make.left.equalTo(_backView.mas_left).offset(WIDTH - 0.75 * FONTSIZE);
        make.right.equalTo(_backView.mas_right).offset(0.75 * FONTSIZE - WIDTH);
        make.height.mas_offset(270 * FONTSIZE);
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
